function newXMLHttpRequest() {
    var xmlreq = false;
    if (window.XMLHttpRequest) {
        // Create XMLHttpRequest object in non-Microsoft browsers
        xmlreq = new XMLHttpRequest();
    }
    else
    if (window.ActiveXObject) {
        // Create XMLHttpRequest via MS ActiveX
        try {
            // Try to create XMLHttpRequest in later versions
            // of Internet Explorer
            xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) {
            alert("Failed to create required ActiveXObject");
            // Failed to create required ActiveXObject
            try {
                // Try version supported by older versions
                // of Internet Explorer
                xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                alert("Unable to create an XMLHttpRequest with ActiveX");
            // Unable to create an XMLHttpRequest with ActiveX
            }
        }
    }
    return xmlreq;
}
function change(parameter) {

    // Obtain an XMLHttpRequest instance
    var req = newXMLHttpRequest();
    req.open("POST", "CheckServlet", true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(parameter + "=" + document.getElementsByName(parameter)[0].value);
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            if (req.status == 200) {
                update(req.responseText, parameter);
            }
            else {
                req.abort();
                alert("HTTP error: "+req.status);
            }
        }
    };
}
function update(res, parameter) {
    document.getElementById(parameter + "Result").innerHTML = res;
}
//var resp = "false";
function isFill() {
    var request = newXMLHttpRequest();
    request.open("POST", "GlobalCheck", false);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send("");
    var resp = request.responseText;
    alert(resp);
    if(resp == "true") {
        return true;
    } else {
        return false;
    }
}

function validUser() {
    var request = newXMLHttpRequest();
    var name = document.getElementsByName("name")[0].value;
    var pass = document.getElementsByName("pass")[0].value;
    request.open("POST", "Login", false);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send("&name=" + name + "&pass=" + pass + "&ch=true");
    var resp = request.responseText;
    if(resp) {
        if(resp == "false") {
            document.getElementById("loginResult").innerHTML = "Wrong login/password";
            return false;
        }
    }
    return true;
}
function validMail() {
    var request = newXMLHttpRequest();
    var mail = document.getElementsByName("mail")[0].value;
    request.open("POST", "RescuePass", false);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send("&mail=" + mail + "&ch=true");
    var resp = request.responseText;
    if(resp) {
        if(resp == "false") {
            document.getElementById("mailResult").innerHTML = "Such mail isn't registered!";
            return false;
        }
    }
    return true;
}
function rmCookie() {
    document.cookie="login=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    window.location.reload();
}
